package com.qinyeit.serviceapp.service.thirdpart;

import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyetech.springstage.core.http.okhttp.HttpUtils;
import com.qinyetech.springstage.core.lang.QyMap;
import lombok.extern.slf4j.Slf4j;
import okhttp3.ResponseBody;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@Transactional
public class BranchPlatformApiServiceImpl {

//    private Log log = LogFactory.getLog(getClass());

    @Value("${branchplatform:}")
    private String branchplatform = "";


    //注册商户
    public Long createBranchInitData(String account, String password, String activeKey,String type, Long merchantGroupId,String phone,String shopName) {
        try{
//            type = "CHINESE_FOOD";
            String url = branchplatform+"/internal/merchant/registerMerchant";
            QyMap map = QyMap.map("account",account)
                    .setv("password",password)
                    .setv("activeKey",activeKey)
                    .setv("type",type)
                    .setv("merchantGroupId",merchantGroupId.toString())
                    .setv("phone",phone)
                    .setv("shopName",shopName);
            ResponseBody body = HttpUtils.post(url).setParams(map)
                    .execute()
                    .body();
            String result = body.string();
            map = QyMap.WRAP(result);
            if(map.getInt("status")!=200){
                throw new BusinessRunTimeException(map.getString("message"), 1001);
            }
            return map.getLong("content");
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessRunTimeException(e.getMessage(), 10002);
        }
    }
    //列表删除商品信息
    public boolean deleteBranchDishesData(String uuid) {
        try{
            String url = branchplatform+"/internal/merchant/deleteDishesList";
            QyMap map = QyMap.map("uuid",uuid);
            ResponseBody body = HttpUtils.post(url).setParams(map)
                    .execute()
                    .body();
            String result = body.string();
            map = QyMap.WRAP(result);
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>1111:{}",map);
            return map.getBoolean("success");
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessRunTimeException(e.getMessage(), 10002);
        }
    }

    //删除一个
    public boolean deleteBranchDishesDataByOne(Long merchantId,String uuid) {
        try{
            String url = branchplatform+"/internal/merchant/deleteDishes";
            QyMap map = QyMap.map("uuid",uuid)
                    .setv("merchantId",merchantId.toString());
            ResponseBody body = HttpUtils.post(url).setParams(map)
                    .execute()
                    .body();
            String result = body.string();
            map = QyMap.WRAP(result);
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>2222:{}",map);
            return map.getBoolean("success");
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessRunTimeException(e.getMessage(), 10002);
        }
    }
}
